home *** CD-ROM | disk | FTP | other *** search
- Path: lantana.singnet.com.sg!usenet
- From: Teddy Bear <s7700038@singnet.com.sg>
- Newsgroups: comp.lang.c
- Subject: Can C save and retreive from disk?
- Date: 8 Mar 1996 13:34:28 GMT
- Organization: Singapore Telecom Internet Service
- Message-ID: <4hpd14$hel@lantana.singnet.com.sg>
- NNTP-Posting-Host: ts900-3729.singnet.com.sg
- Mime-Version: 1.0
- Content-Type: multipart/mixed;
- boundary="-------------------------------300002593617914"
- X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
-
- This is a multi-part message in MIME format.
-
- ---------------------------------300002593617914
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
-
- Can anyone teach me or refer me or help in any way?
- I need to save to disk and retrieve from disk
- Can anyone help me
- The prog goes
-
- ---------------------------------300002593617914
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain
-
- /* This program simply enable the user to enter the particular(s). It will
- then sort the name in an alphabetical order and display the particular(s)
- on the screen. It also ensures that the user is able to add and delete
- the particular(s). */
-
- #include <stdio.h>
- #include <alloc.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
- #define TRUE 1
- #define FALSE 0
-
- typedef struct data{
- int age;
- char name[40];
- char address[100];
- struct data *next;
- }dataelm;
- typedef dataelm* dataptr;
-
- void createdata(dataptr *ptr_to_head, dataptr head);
- int countlink(dataptr head);
- void sortlink(dataptr head);
- dataptr searchlink(dataptr head, char *wanted);
- void display(dataptr head);
- void save(dataptr headptr, char file[20]);
- void read(char file[20]);
- dataptr clearlink(dataptr head);
- dataptr addnode(dataptr head);
- dataptr delnode(dataptr head);
- void check(dataptr head);
-
- int main()
- {
- char *sname, option;
- int checker = 0;
- dataptr ptr, *ptr_to_head, head, tail;
-
- head = NULL; /* assigns head with NULL, starting of link list */
- option = '1';
- do
- {
- clrscr();
- printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ MAIN MENU ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ\n");
- printf("\n1) Adding of particular(s).\n");
- printf("2) Adding of new particular(s).\n");
- printf("3) Deleting of current particular(s).\n");
- printf("4) Display all the particular(s) entered.\n");
- printf("5) Exit the program.\n");
- printf("\nPlease enter your choice : ");
- if(option < '0' || option > '6')
- printf("\nInvaild option. Please re-enter.");
- option = getche();
- switch(option)
- {
- case '1' : if(checker != 0)
- {
- printf("\a");
- printf("\n\nYou had create a link list previously.");
- printf("\nIf you proceed, the previous data will be lost.");
- printf("\n\nEnter a 'Y' to proceed.");
- printf("\nEnter ESC to return to main menu ");
- if((toupper(getch())) != 'Y')
- break;
- else
- clearlink(head);
- }
- createdata(ptr_to_head, NULL);
- head = *ptr_to_head;
- sortlink(head);
- display(head);
- checker = 1;
- break;
- case '2' : if(checker != 1)
- {
- printf("\a");
- printf("\n\nYou have not yet create a link list.");
- printf("\nPlease choose option '1'.");
- printf("\n\nPress any key to continue.");
- getch();
- break;
- }
- head = addnode(head);
- checker = 1;
- display(head);
- break;
- case '3' : if(checker!=1)
- {
- printf("\a");
- printf("\n\nYou have not yet create a link list.");
- printf("\nPlease choose option '1'.");
- printf("\n\nPress any key to continue.");
- getch();
- break;
- }
- display(head);
- head = delnode(head);
- display(head);
- if(head == NULL)
- checker = 0;
- break;
- case '4' : display(head);
- break;
- case '5' : printf("\n\nHave a nice day. BYE!\n");
- printf("Press any key to end...");
- getch();
- exit(0);
- }
- }
- while(option != '5');
- return 0;
- }
-
- void createdata(dataptr *ptr_to_head, dataptr head)
- /* create a link of the telephone list */
- {
- dataptr temp, last;
- char ans;
- int elm_size = sizeof(dataelm);
- do
- {
- temp = (dataptr)malloc(elm_size);
- clrscr();
- printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ PARTICULAR CREATING ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ\n");
- printf("\nEnter name to be added : ");
- gets(temp->name);
- printf("Enter Age : ");
- scanf("%d",&temp->age);
- fflush(stdin);
- printf("Enter Address : ");
- gets(temp->address);
-
- if(head == NULL)
- {
- head = temp; /* assigns the address of dataptr to head */
- last = temp;
- }
- else
- {
- last->next=temp;
- last = temp;
- }
- printf("\nAny more you would like to enter ? <Y/N> : ");
- ans=toupper(getch());
- }
- while(ans != 'N');
- last->next = NULL;
- *ptr_to_head = head;
- }
-
- void sortlink(dataptr head)
- /* sort the link in an alphabetical order */
- {
- dataptr front, back;
- struct data temp;
- int pass, cnt, condition;
- int element;
- element = countlink(head);
- pass = 0;
- do
- {
- pass++;
- condition = TRUE;
- front = head->next;
- back = head;
- for(cnt = 0; cnt < element - pass; cnt++)
- {
- /* compare the character */
- if((strcmp((back->name), (front->name))) > 0)
- {
- condition = FALSE;
- strcpy(temp.name, back->name);
- temp.age = back->age;
- strcpy(temp.address, back->address);
- strcpy(back->name, front->name);
- back->age = front->age;
- strcpy(back->address, front->address);
- strcpy(front->name, temp.name);
- front->age = temp.age;
- strcpy(front->address, temp.address);
- }
- back = front;
- front = front->next;
- }
- }
- while(condition==FALSE);
- }
-
- dataptr searchlink(dataptr head, char *wanted)
- /* search the link for an element */
- {
- int element, count, found, condition;
- dataptr ptr;
- element = countlink(head);
- ptr = head;
- found = FALSE;
- condition = TRUE;
- for(count = 0; (count < element) && (condition); count++)
- {
- if((strcmp(ptr->name, wanted)) == 0)
- {
- found = TRUE;
- condition = FALSE;
- }
- else
- if((strcmp(ptr->name, wanted)) > 0)
- condition = FALSE;
- else
- {
- ptr = ptr->next;
- count++;
- }
- }
-
- if(found)
- return ptr;
- else
- return NULL;
- }
-
- void display(dataptr head)
- /* display the particular(s) in the link */
- {
- int x = 0;
- int y = 6;
- dataptr ptr;
- ptr = head;
- clrscr();
- gotoxy(1,3);printf("NAME AGE ADDRESS");
- gotoxy(1,4);printf("");
- gotoxy(1,2);printf("");
- while(ptr!=NULL)
- {
- gotoxy(x+1,y);printf("%s",ptr->name);
- gotoxy(x+20,y);printf("%d ",ptr->age);
- gotoxy(x+28,y);printf("%s",ptr->address);
- y++;
- ptr = ptr->next;
-
- if(wherey() > 20) /* gives current vertical cursor position */
- {
- printf("\nPress any key to view next page.");
- getch();
- clrscr();
- }
- }
- printf("\n\nPress any key to continue.. ");
- getch();
- }
-
- dataptr clearlink(dataptr head)
- /* clear all data and free all memory used by the data */
- {
- dataptr ptr;
- while(head != NULL)
- {
- ptr = head;
- head = head->next;
- free(ptr);
- }
- free(ptr);
- return head;
- }
-
- dataptr addnode(dataptr head)
- /* add a node to the link */
- {
- dataptr front, back, newitem;
- int check, cnt;
- newitem = (dataptr)malloc(sizeof(dataelm));
- clrscr();
- sortlink(head);
- printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ PARTICULAR ADDING ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ");
- printf("\n\nEnter name to be added : ");
- gets(newitem->name);
- if((strcmp((back->name),(front->name))) == 0)
- {
- printf("\a");
- printf("\n\nDUPLICATE COPY");
- strcpy(back->name,front->name);
- back->age = front->age;
- strcpy(back->address,front->address);
- getch();
- }
- fflush(stdin);
- printf("Enter age : ");
- scanf("%d", &newitem->age);
- fflush(stdin);
- printf("Enter address : ");
- gets(newitem->address);
- if((strcmp(newitem->name, head->name)) <= 0)
- {
- newitem->next = head;
- head = newitem;
- }
- else
- {
- back = head;
- front = back->next;
- while(front != NULL)
- {
- if((strcmp(newitem->name, front->name)) > 0)
- {
- back = front;
- front = front->next;
- }
- else
- break;
- }
- newitem->next = front;
- back->next = newitem;
- }
- return head;
- }
-
- dataptr delnode(dataptr head)
- /* delete a node from a link */
- {
- dataptr front, back;
- char *tempname;
- if(head != NULL)
- {
- back = head;
- front = back->next;
- }
- else
- {
- printf("\a\nThere is nothing to be deleted in the link list.");
- getch();
- return NULL;
- }
- tempname = (char *)malloc(sizeof(char *));
- clrscr();
- printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ DATA TO BE DELETED ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ");
- printf("\n\nEnter name to be delete : ");
- gets(tempname);
- if((strcmp(tempname,back->name)) == 0)
- {
- front = head;
- back = back->next;
- free(front);
- return back;
- }
- else
- {
- while(((strcmp(tempname, front->name)) != 0) && (front != NULL))
- {
- back = front;
- front = front->next;
- }
- if(front == NULL)
- {
- printf("\a\nThere is no such name in the link.");
- getch();
- return head;
- }
- else
- {
- back->next = front->next;
- free(front);
- return head;
- }
- }
-
- }
-
- int countlink(dataptr head)
- /* count the number of elements in the link */
- {
- int count;
- dataptr ptr;
- ptr = head;
- if(ptr != NULL)
- {
- for(count = 1; ptr->next != NULL; count++)
- ptr = ptr->next;
- return count;
- }
- else
- {
- return 0;
- }
- }
-
- void save(dataptr headptr, char file[20])
- /* save particular(s) entered */
- {
- FILE *f;
- dataptr ptr;
- ptr = headptr;
- if(ptr == NULL)
- {
- printf("\aWarning! There is no link list to be saved\n");
- }
- else
- {
- printf("Enter a name for the file to be saved : ");
- flushall();
- gets(file);
- f = fopen(file, "w");
- while(ptr != NULL)
- {
- fprintf(f,"\nName : %s\n", ptr->name);
- fprintf(f,"Age : %d\n", ptr->age);
- fprintf(f,"Address : %s\n", ptr->address);
- ptr = ptr->next;
- }
- printf("The data has been saved.\n");
- fclose(f);
- }
- return;
- }
-
- void read(char file[20])
- /* read particular(s) */
- {
- FILE *f;
- char msg[20];
- printf("Enter the name of the file to be read : ");
- flushall();
- gets(file);
- if((f = fopen(file, "r")) == NULL)
- {
- printf("\aError! Cannot open file! File does not exist.\n");
- }
- else
- {
- printf("The file consists of : ");
- while(feof(f) == 0)
- {
- fgets(msg, 2, f);
- printf("%s", msg);
- }
- fclose(f);
- }
- return;
- }
-
- ---------------------------------300002593617914--
-